﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web; 
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;

namespace Vision.Utilities.CMS6UpdateTool
{
    public class AutoUpdateSiteFileProcessorConfigurationElement : AutoUpdateProcessorConfigurationElement<IUpdateProcessor>
    {
        #region Const
        const string PackageFolderPropertyName = "packageFolder";
        const string StepsPropertyName = "steps";
        #endregion

        #region Ctors
        public AutoUpdateSiteFileProcessorConfigurationElement()
            : base()
        {
        }

        public AutoUpdateSiteFileProcessorConfigurationElement(string name, Type type)
            : base(name, type)
        {
        }
        #endregion

        #region Properties
        [ConfigurationProperty(PackageFolderPropertyName)]
        public string PackageFolder
        {
            get { return (string)this[PackageFolderPropertyName]; }
            set { this[PackageFolderPropertyName] = value; }
        }

        [ConfigurationCollection(typeof(SiteFileOperateStepConfigurationElement))]
        [ConfigurationProperty(StepsPropertyName, IsDefaultCollection = false)]
        public SiteFileOperateStepConfigurationCollection Steps
        {
            get { return (SiteFileOperateStepConfigurationCollection)this[StepsPropertyName]; }
            set { this[StepsPropertyName] = value; }
        }
        #endregion

        #region Create Instance
        public override IUpdateProcessor CreateInstance()
        {
            var result = new SiteFileUpdateProcessor(Steps.Cast<SiteFileOperateStepConfigurationElement>().Select(c => c.CreateOperator()).ToArray(), PackageFolder, Order);
            return result;
        }
        #endregion
    }
}